package com.sanxiau.dormitory.yang.service.impl;

import com.sanxiau.dormitory.yang.entity.Dorm;
import com.sanxiau.dormitory.yang.entity.Dorout;
import com.sanxiau.dormitory.yang.entity.LoginIn;
import com.sanxiau.dormitory.yang.mapper.DoroutMapper;
import com.sanxiau.dormitory.yang.service.DoroutService;
import com.sanxiau.dormitory.yang.util.GetAfterDay;
import com.sanxiau.dormitory.yang.vo.ResultInfo;
import org.jacoco.agent.rt.internal_43f5073.core.internal.flow.IFrame;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/*寝室考勤Service层实现类代码*/
@Service
public class DoroutServiceImpl implements DoroutService {
    @Autowired
    DoroutMapper doroutMapper;

    @Override
    @Transactional
    public ResultInfo setCheckTime(List<Date> dates, String start, String end, HttpSession session) {
        ResultInfo resultInfo = new ResultInfo();
        String reslut = "";
        LoginIn loginIn = doroutMapper.queryUserByName(session.getAttribute("userName")+"");
        List<Dorm> dorms = doroutMapper.queryAllDorm();
        List<Dorout> dorouts = new ArrayList<>();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat ds = new SimpleDateFormat("yyyy-MM-dd ");
        Date startTime = new Date();
        Date endTime = new Date();
        if (null != dorms){
            for (Date date : dates) {
                try {
                    startTime = df.parse(ds.format(date)+start+":00");
                    endTime = df.parse(ds.format(date)+end+":00");
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                for (Dorm dorm : dorms) {
                    Dorout dorout = new Dorout();
                    dorout.setHou_id(dorm.getHou_id());
                    dorout.setDor_id(dorm.getDor_id());
                    dorout.setOut_sf("false");
                    dorout.setOut_nub(dorm.getDor_price());
                    dorout.setOut_why("");
                    dorout.setWatch(loginIn.getTrue_name());
                    dorout.setOut_date(date);
                    dorout.setCheck_start(startTime);
                    dorout.setCheck_end(endTime);
                    dorout.setOut_des("");
                    dorouts.add(dorout);
                }
            }
            for (int i = 0; i < dorouts.size(); i++) {
                Dorout dorout = doroutMapper.queryDoroutExist(dorouts.get(i));
                if (null==dorout){

                }else {
                    int num = doroutMapper.updateDorout(dorouts.get(i));
                    if (num > 0){
                        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                        Date date = new Date();
                        int num1 = doroutMapper.deleteAttendances(dorouts.get(i).getHou_id(),dorouts.get(i).getDor_id(),format.format(date), GetAfterDay.getSpecifiedDayAfter(format.format(date)));
                        dorouts.remove(i);
                        i--;
                    }

                }
            }
            if (null == dorouts || dorouts.size() ==0){
                resultInfo.setMessage("设置成功");
            }else {
                int num = doroutMapper.addDorouts(dorouts);
                if (num == dorouts.size()) {
                    resultInfo.setMessage("设置成功");
                } else {
                    try {
                        throw new Exception("添加考勤记录查询问题，抛出异常，进行事务回滚");
                    } catch (Exception e) {
                        resultInfo.setMessage("成功设置" + num + "条记录," + "失败" + (dorouts.size() - num) + "条记录。设置时出现错误，请重新设置");
                        e.printStackTrace();
                        return resultInfo;
                    }
                }
            }
        }else {
            System.out.println("设置考勤时间出错----查询寝室出错");
            resultInfo.setMessage("设置考勤时间出错，请重试");
        }
        return resultInfo;
    }

    @Override
    @Transactional
    public ResultInfo isDoroutExist(List<Date> dates, String start, String end, HttpSession session) {
        ResultInfo resultInfo = new ResultInfo();
        String reslut = "";
        LoginIn loginIn = doroutMapper.queryUserByName(session.getAttribute("userName")+"");
        List<Dorm> dorms = doroutMapper.queryAllDorm();
        List<Dorout> dorouts = new ArrayList<>();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat ds = new SimpleDateFormat("yyyy-MM-dd ");
        Date startTime = new Date();
        Date endTime = new Date();
        if (null != dorms){
            for (Date date : dates) {
                try {
                    startTime = df.parse(ds.format(date)+start+":00");
                    endTime = df.parse(ds.format(date)+end+":00");
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                for (Dorm dorm : dorms) {
                    Dorout dorout = new Dorout();
                    dorout.setHou_id(dorm.getHou_id());
                    dorout.setDor_id(dorm.getDor_id());
                    dorout.setOut_sf("false");
                    dorout.setOut_nub(dorm.getDor_price());
                    dorout.setOut_why("");
                    dorout.setWatch(loginIn.getTrue_name());
                    dorout.setOut_date(date);
                    dorout.setCheck_start(startTime);
                    dorout.setCheck_end(endTime);
                    dorout.setOut_des("");
                    dorouts.add(dorout);
                }
            }
            for (int i = 0; i < dorouts.size(); i++) {
                Dorout dorout1 = doroutMapper.queryDoroutExist(dorouts.get(i));
                if (null==dorout1){

                }else {
                    int a = reslut.indexOf(ds.format(dorout1.getOut_date()) + "");
                    if (a<0){
                        reslut += ds.format(dorout1.getOut_date());
                    }
                }
            }
            if (reslut.equals("")) {
                if (null == dorouts || dorouts.size() ==0){

                }else {
                    int num = doroutMapper.addDorouts(dorouts);
                    if (num == dorouts.size()) {
                        resultInfo.setMessage("设置成功");
                    } else {
                        try {
                            throw new Exception("添加考勤记录查询问题，抛出异常，进行事务回滚");
                        } catch (Exception e) {
                            resultInfo.setMessage("成功设置" + num + "条记录," + "失败" + (dorouts.size() - num) + "条记录。设置时出现错误，请重新设置");
                            e.printStackTrace();
                            return resultInfo;
                        }
                    }
                }
            }else {
                reslut += "这些日期的考勤已经已经设置过了，是否重新设置";
                resultInfo.setMessage(reslut);
                resultInfo.setIsexist(true);
            }
        }else {
            System.out.println("设置考勤时间出错----查询寝室出错");
            resultInfo.setMessage("设置考勤时间出错，请重试");
        }
        return resultInfo;
    }

    @Override
    public List<Dorout> allDorout() {
        List<Dorout> dorouts = doroutMapper.queryAllDorout();
        return dorouts;
    }

}
